package org.yi.fc.ext.beetl.tag;

import java.util.List;

import org.beetl.core.GeneralVarTagBinding;
import org.yi.fc.entity.FColumn;

import com.google.common.base.Strings;

/**
 * 获取栏目的信息并组装成option标签输出(最多支持五级栏目)
 * @author ilgqh
 *
 */
public class ColumnOptionTag  extends GeneralVarTagBinding{

	/**
	 * 用法：<br/>
	 * <#fc:fc:columnOption columnNo="xxx"/> 
	 * <br/>
	 * columnNo 栏目编号<br/>
	 */
	@Override
	public void render() {
		//获取栏目编号
		String columnNo = (String)this.getAttributeValue("column_no");
		
		if(!Strings.isNullOrEmpty(columnNo)){
			printChildColumnOption(columnNo,"&nbsp;");
		}
		
	}
	
	/**
	 * 输出所有子栏目的信息
	 * @param columnNo    子栏目编号
	 */
	public void printChildColumnOption(String columnNo,String tabStyle){
		try{
			
			List<FColumn> list = FColumn.dao.getNextLevelChildrenFColumnsByParentCode(columnNo);
			
			for(FColumn column : list){
			
				ctx.byteWriter.writeString("<option value='" + column.get("columnNo") + "'>" + tabStyle + "┃┝" + column.get("columnNameCN") +"</option>");

				List<FColumn> childList = FColumn.dao.getNextLevelChildrenFColumnsByParentCode((String) column.get("columnNo") );
				
				if(childList.size() > 0 ){
					//递归
					for(FColumn childColumn : childList){
						ctx.byteWriter.writeString("<option value='" + childColumn.get("columnNo") + "'>" + tabStyle + "&nbsp;&nbsp;&nbsp;&nbsp;" + "┃┝" + childColumn.get("columnNameCN") +"</option>");
						printChildColumnOption((String)childColumn.get("columnNo"), tabStyle + tabStyle  + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" );
					}
				}
				this.doBodyRender();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				ctx.byteWriter.flush();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
}
